<?php
/**
 * UserInvite.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-15 下午1:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class UserInvite extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'UserInvite',
        'fields' => [
            'InviteId' => self::VAR_TYPE_KEY,
            'UserId' => self::VAR_TYPE_STRING,
            'Mobile' => self::VAR_TYPE_STRING,
            'Status' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'UserInvite:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    /**
     * 新增邀请关系
     *
     * @param $uid
     * @param $mobile
     * @return bool|int|string
     */
    public static function addInvite($uid, $mobile)
    {
        if (empty($uid) || empty($mobile)) {
            return false;
        }
        $data = [
            'UserId' => $uid,
            'Mobile' => $mobile
        ];

        return parent::add($data, true);
    }

    /**
     * 邀请成功更新
     *
     * @param $id
     * @return bool
     */
    public static function inviteSuccess($id)
    {
        return parent::update($id, ['Status' => 1]);
    }

    /**
     * 用户注册时检查邀请关系
     *
     * @param $mobile
     * @return array
     */
    public static function getInfoByMobile($mobile)
    {
        $where = [
            'Mobile' => $mobile
        ];
        $list_info = self::getList($where, 0, 1, '*', 'InviteId ASC');

        return !empty($list_info['data'][0]) ? $list_info['data'][0] : [];
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}